import 'package:catguard/config/config.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:line_awesome_flutter/line_awesome_flutter.dart';

import 'home_logic.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final logic = Get.find<HomeLogic>();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      // 页面完全加载完毕后的回调
      Future.delayed(const Duration(milliseconds: 500),(){
        setState(() {
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: true,
      onPopInvoked: (didPop) async {},
      child: Scaffold(
        backgroundColor: Config.colorBg,
        resizeToAvoidBottomInset: false,
        body: Obx(
          () => IndexedStack(
            index: logic.currentIndex.value,
            children: logic.pages,
          ),
        ), // 根据当前索引切换页面
        bottomNavigationBar: Obx(() => BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              currentIndex: logic.currentIndex.value,
              elevation: 1,
              selectedItemColor: Config.colorText1,
              backgroundColor: Config.colorBg2,
              unselectedItemColor: Config.colorText3.withOpacity(0.8),
              onTap: (int index) {
                setState(() {
                  logic.currentIndex.value = index; // 更新选中索引
                });
              },
              iconSize: 52.w,
              items: const [
                BottomNavigationBarItem(
                  icon: Icon(LineAwesomeIcons.cat_solid),
                  label: '首页',
                ),
                BottomNavigationBarItem(
                  icon: Icon(LineAwesomeIcons.evernote),
                  label: '手账',
                ),
                BottomNavigationBarItem(
                  icon: Icon(LineAwesomeIcons.user_circle),
                  label: '我的',
                ),
              ],
            )),
      ),
    );
  }
}
